package org.sunrise.game.common.network.thread;

import lombok.Setter;
import org.sunrise.game.common.log.LogCore;

public class DispatchThread {
    private final Thread thread;
    private final Runnable task;
    @Setter
    private int interval = 1;

    public DispatchThread(Runnable task) {
        this.task = task;
        this.thread = new Thread(this::run);
    }

    public DispatchThread(Runnable task, String name) {
        this.task = task;
        this.thread = new Thread(this::run, name);
    }

    private void run() {
        while (true) {
            try {
                task.run();
                Thread.sleep(interval);
            } catch (Exception e) {
                LogCore.Server.error("DispatchThread pulse, error : ", e);
            }
        }
    }

    public void start() {
        this.thread.start();
        LogCore.ServerStartUp.info("DispatchThread Start, name = { {} }, { {} }", thread.getName(), thread);
    }
}
